package com.one.group.admin.controller;

import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.*;
import com.one.group.model.entity.SpHouse;
import com.one.group.model.vo.HouseManagementPlusVo;
import com.one.group.model.vo.HouseManagementVo;
import com.one.group.result.Result;
import com.one.group.service.SpHouseService;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spHouse")
@Tag(name = "房屋接口")
public class SpHouseController {
    @Autowired
    private SpHouseService spHouseService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @GetMapping("/HouseManagement")
    @Operation(summary = "查询房屋管理列表")
    public Result HouseManagement(Long buildingId){
        //创建实体类
        HouseManagementPlusVo houseManagementPlusVo = new HouseManagementPlusVo();
        //查询楼栋单元列表
        List<String> unitList = spHouseService.getunitList(buildingId);
        houseManagementPlusVo.setUnitList(unitList);
        //查询楼栋楼层列表
        List<String> floorList = spHouseService.getfloorList(buildingId);
        houseManagementPlusVo.setFloorList(floorList);
        //查询房屋列表
        List<HouseManagementVo> houseList = spHouseService.getHouseManagementList(buildingId);
        houseManagementPlusVo.setRoomList(houseList);
        return ResultUtil.success(houseManagementPlusVo);
    }
    @PostMapping("/add")
    @Operation(summary = "添加房屋")
    public Result add(@RequestBody SpHouseAddDto spHouseAddDto){
        if (spHouseService.myadd(spHouseAddDto)){
            return ResultUtil.success();
        }
        return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @DeleteMapping("/delete")
    @Operation(summary = "删除房屋")
    public Result delete(@RequestBody BaseDeleteDto baseDeleteDto){
        if (spHouseService.removeByIds(baseDeleteDto.getIds())){
            rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
            rabbitTemplate.convertAndSend("gxa.direct","allHouseUpdate",1);
            return ResultUtil.success();
        }
        return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @GetMapping("/getById")
    @Operation(summary = "查询房屋")
    public Result getById(Long id){
        return ResultUtil.success(spHouseService.getById(id));
    }
    @PutMapping("/update")
    @Operation(summary = "更新房屋")
    public Result update(@RequestBody SpHouseUpdateDto spHouseUpdateDto){
        if (spHouseService.myUpdate(spHouseUpdateDto)){
            return ResultUtil.success();
        }
        return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @DeleteMapping("/deleteAll")
    @Operation(summary = "删除所有房屋")
    public Result deleteAll(Long buildingId){
        if (spHouseService.removeByBuildingId(buildingId)){
            return ResultUtil.success();
        }
        return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/addAllHouse")
    @Operation(summary = "添加所有房屋")
    public Result addAllHouse(@RequestBody SpHouseAddPlusDto spHouseAddPlusDto){
        if (spHouseService.addAllHouse(spHouseAddPlusDto)){
            return ResultUtil.success();
        }
        return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/queryPersonnelByHouseId")
    @Operation(summary = "查询房屋人员")
    public Result queryPersonnelByHouseId(@RequestBody QueryPersonnelDto queryPersonnelDto ){
        if(queryPersonnelDto.getOwnerId() == null){
            return ResultUtil.success(null);
        }else {
            return ResultUtil.success(spHouseService.queryPersonnelByHouseId(queryPersonnelDto));
        }

    }
}
